import java.math.BigDecimal;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.mfjassociates.finance.domain.mfjfin.ExpenseDetails;
import net.mfjassociates.finance.domain.mfjfin.ExpenseReports;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;


public class TestJpa {
	public static void main(String[] args) {
//		displayEntityManager("1=");
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
//		displayEntityManager("2=");
//		ClassPathXmlApplicationContext ctx2 = new ClassPathXmlApplicationContext("META-INF/spring2/applicationContext.xml");
//		displayEntityManager("3=");
		TestJpa tjpa=new TestJpa();
		tjpa.createRecord(1);
//		tjpa.createPersonRecord();
		tjpa.display();
		tjpa.displayReference();
//		tjpa.displayOne();
//		tjpa.displayPerson();
	}
	@Transactional
	private void displayReference() {
		List<ExpenseReports> rep=ExpenseReports.findAllExpenseReportses();
		for (ExpenseReports er : rep) {
			System.out.println("no="+er.getExpenseReportId()+", name='"+er.getExpenseRptName()+"'");
			displayERReference(er);
		}
	}
	@Transactional
	public void display() {
		List<ExpenseReports> rep=ExpenseReports.findAllExpenseReportses();
		for (ExpenseReports er : rep) {
//			System.out.println(er);
			System.out.println("no="+er.getExpenseReportId()+", name='"+er.getExpenseRptName()+"'");
			displayER(er);
		}
	}
	@Transactional
	private void displayOne() {
		ExpenseReports er=ExpenseReports.findExpenseReports(2);
		System.out.println(er);
	}
	
//	@Transactional
//	private void createPersonRecord() {
//		Person p=createPerson();
//		displayPerson();
//	}
	@Transactional
	private void createRecord(int dbno) {
		
		ExpenseReports er1=new ExpenseReports();
		ExpenseReports er2=new ExpenseReports();
		er1=createRecord1(1);
		er2=createRecord2(1);
		er1.flush();
		er2.flush();

		display();
	}
	
//	@Transactional
//	private void displayPerson() {
//		Person p=Person.findPeopleByNameEquals("Rob").getSingleResult();
//		System.out.println(p);
//	}

	private ExpenseReports createRecord1(int dbno) {
		ExpenseReports er=new ExpenseReports();
		ExpenseDetails d=new ExpenseDetails();
		er.persist();
		d.persist();
		er.setExpenseRptName("Test in db "+dbno);
		er.setDateSubmitted(new Date());
		d.setExpenseDate(new GregorianCalendar(2012, 2, 4, 0, 34, 32).getTime());
		d.setExpenseItemAmount(BigDecimal.valueOf(12.32d));
		d.setExpenseItemDescription("hired a cab");
		d.setExpenseReportId(er);
		return er;
	}
	private ExpenseReports createRecord2(int dbno) {
		ExpenseReports er=new ExpenseReports();
		ExpenseDetails d;
		er.persist();
		er.setExpenseRptName("Test in db "+dbno);
		er.setDateSubmitted(new Date());
		d=new ExpenseDetails();
		d.persist();
		d.setExpenseDate(new GregorianCalendar(2012, 2, 4, 0, 34, 32).getTime());
		d.setExpenseItemAmount(BigDecimal.valueOf(12.32d));
		d.setExpenseItemDescription("hired a cab");
		d.setExpenseReportId(er);
		d=new ExpenseDetails();
		d.persist();
		d.setExpenseDate(new GregorianCalendar(2012, 2, 4, 5, 6, 43).getTime());
		d.setExpenseItemAmount(BigDecimal.valueOf(15.65d));
		d.setExpenseItemDescription("Cannot tell");
		d.setExpenseReportId(er);
		return er;
	}
//	private Person createPerson() {
//		Phone[] pa=new Phone[2];
//		pa[0]=new Phone();
//		pa[0].setNum("123");
//		pa[1]=new Phone();
//		pa[1].setNum("456");
//		pa[0].persist();
//		pa[1].persist();
//		Person p=new Person();
//		p.setName("Rob");
//		p.setAge(5);
//		p.getPhones().add(pa[0]);
//		p.getPhones().add(pa[1]);
//		pa[0].setOwner(p);
//		pa[1].setOwner(p);
//		p.persist();
//		p.flush();
//		return p;
//	}
	private void displayERReference(ExpenseReports er) {
		// TODO Auto-generated method stub
		
	}
	private void displayER(ExpenseReports er) {
//		Set<ExpenseDetails> items= er.getExpenseDetailss();
//		for (ExpenseDetails ed : items) {
//			System.out.println(ed.getExpenseDate()+"', amount="+ed.getExpenseItemAmount()+", desc='"+ed.getExpenseItemDescription()+"'");
//		}
	}

}
